package com.ls.markt.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ls.markt.model.admin.Trophy;
import com.ls.markt.service.admin.ClubService;
import com.ls.markt.service.admin.CoachService;
import com.ls.markt.service.admin.EnumManageItemService;
import com.ls.markt.service.admin.TrophyService;
import com.ls.markt.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @author ls
 * @descrption 奖杯管理
 */
@Controller
@RequestMapping("/trophy")
public class TrophyController {

    @Autowired
    private TrophyService trophyService;

    @Autowired
    private EnumManageItemService enumManageItemService;

    @Autowired
    private CoachService coachService;

    @Autowired
    private ClubService clubService;

    @GetMapping("/index")
    public String index(){
        return "admin/trophy/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public JSONObject list(@RequestParam(value="page",required=false,defaultValue="1")int page,
                                   @RequestParam(value="rows",required=false,defaultValue="10")int rows){

        PageRequest pageRequest = PageRequest.of(page-1,rows);
        Page<Trophy> trophies = trophyService.listTrophys(pageRequest);

        JSONArray jsonArray = new JSONArray();
        if (trophies.getSize()>0){
            for (Trophy trophy:trophies){
                JSONObject trophyJson = (JSONObject) JSONObject.toJSON(trophy);
                trophyJson.put("clubId",clubService.getClubName(trophy.getClubId()));
                trophyJson.put("trophyId",enumManageItemService.getNameById(trophy.getTrophyId()));
                trophyJson.put("season",enumManageItemService.getNameById(trophy.getSeason()));
                trophyJson.put("coach",coachService.getNameById(trophy.getCoachId()));
                jsonArray.add(trophyJson);
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",trophies.getSize());
        jsonObject.put("rows",jsonArray);
        return jsonObject;
    }

    @GetMapping("/add")
    public String add(){
        return "admin/trophy/add";
    }

    @PostMapping("/save")
    @ResponseBody
    public Response save(Trophy trophy){
        trophyService.saveTrophy(trophy);
        return new Response(true);
    }

    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable("id")Long id, Model model){
        Trophy trophy = trophyService.getTrophyById(id);
        model.addAttribute("trophy",trophy);
        return new  ModelAndView("admin/trophy/edit","trophyModel",model);
    }

    /**
     * 执行删除球员
     * @param id
     * @return
     */
    @DeleteMapping("/doDelete/{id}")
    @ResponseBody
    public Response doDelete(@PathVariable("id")Long id){
        trophyService.removeTrophy(id);
        return new Response(true);
    }

    /**
     * 查询该俱乐部 有哪些奖杯 和数量
     * @param clubId 俱乐部id
     * @return
     */
    @RequestMapping("/listByClub/{clubId}")
    @ResponseBody
    public JSONObject listTrophyByClub(@PathVariable("clubId")Long clubId){
        List<Map<String,Object>> trophies = trophyService.getTrophyByClub(clubId);
        JSONArray jsonArray = new JSONArray();
        if (trophies.size()>0){
            for (Map<String,Object> map :trophies){
                JSONObject trophyJson = new JSONObject();
                trophyJson.put("count",map.get("count"));
                trophyJson.put("name",enumManageItemService.getNameById((Long) map.get("tro")));
                jsonArray.add(trophyJson);
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",trophies.size());
        jsonObject.put("rows",jsonArray);
        return jsonObject;
    }

    @RequestMapping("/listByChampion")
    @ResponseBody
    public JSONObject listByChampion(@RequestParam(value = "type",required = true)String type){
        Long trophyId = enumManageItemService.getIdByName(type);
        List<Trophy> trophies = trophyService.listTrophyByEnum(trophyId);
        JSONArray jsonArray = new JSONArray();
        if (trophies.size()>0){
            for (Trophy trophy:trophies){
                JSONObject trophyJson = (JSONObject)JSONObject.toJSON(trophy);
                trophyJson.put("clubId",clubService.getClubName(trophy.getClubId()));
                trophyJson.put("season",enumManageItemService.getEnumManageItemById(trophy.getSeason()).getName());
                trophyJson.put("coach",coachService.getNameById(trophy.getCoachId()));
                jsonArray.add(trophyJson);
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",trophies.size());
        jsonObject.put("rows",jsonArray);
        return jsonObject;
    }


}
